package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.ReactiveValueOperationsExtensionsKt;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/24 18:40
 * @Version 1.0
 */

@Component
public class UserManager {


    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private MovementService movementService;

    @DubboReference
    private CommentService commentService;

    @DubboReference
    private VideoService videoService;

    //用户列表
    public ResponseEntity users(Integer pageNum, Integer pageSize) {

        //rpc远程调用
        PageBeanVo pageBeanVo = userInfoService.findUserInfoByPage(pageNum, pageSize);

        return ResponseEntity.ok(pageBeanVo);

    }

    // 用户详情
    public ResponseEntity findUserInfoById(Long userId) {

        UserInfo userInfo = userInfoService.findById(userId);

        return ResponseEntity.ok(userInfo);
    }


    //动态查询
    public ResponseEntity messages(Long userId, Integer stateInt, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = movementService.findMovementByPage(userId, stateInt, pageNum, pageSize);

        //构造vo集合
        ArrayList<MovementVo> voList = new ArrayList<>();

        //遍历集合封装vo
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(movementList)) {

            for (Movement movement : movementList) {

                //封装vo,注意封装顺序
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfoService.findById(movement.getUserId()));
                vo.setMovement(movement);
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
                voList.add(vo);
            }

        }

        //voList设置到分页对象
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);

    }


    //动态详情
    public ResponseEntity findMovementVoById(String publishId) {

        // 1.调用rpc查询
        Movement movement = movementService.findById(new ObjectId(publishId));

        if (ObjectUtil.isEmpty(movement)) {

            return null;

        }
        // 2.封装vo
        // 创建vo
        MovementVo vo = new MovementVo();
        // 先userInfo
        vo.setUserInfo(userInfoService.findById(movement.getUserId()));
        // 再movement
        vo.setMovement(movement);
        // 设置具体动态时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
        // 3.返回vo
        return ResponseEntity.ok(vo);


    }



    //评论列表
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        //调用rpc
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(publishId), 2, pageNum, pageSize);

        //封装commentVo
        List<CommentVo> voList = new ArrayList<>();
        //遍历集合封装vo
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();


        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {

                CommentVo vo = new CommentVo();
                //先userInfo
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                //再comment
                vo.setId(comment.getId().toHexString());
                vo.setContent(comment.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                voList.add(vo);
            }

        }

        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);


    }


    //小视频查询
    public ResponseEntity findVideoVoById(Long userId, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = videoService.findVideoByPage(userId, pageNum, pageSize);

        //创建vo集合
        ArrayList<VideoVo> voList = new ArrayList<>();
        //遍历集合封装vo
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {

            for (Video video : videoList) {

                VideoVo vo = new VideoVo();
                vo.setUserInfo(userInfoService.findById(userId));
                vo.setVideo(video);

                voList.add(vo);
            }

        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);

    }
}
